﻿/*------------------------------------图片操作------------------------------------------*/
var Album =
{
    NextUrl: "",
    FrontUrl: "",
    FinishUrl: "",
    DeleteImagePost: "",
    SaveImageItemPost: "",
    ProductId: "",
    FileBrowserUrl: "",
    ItemUpdatePost: "",
    SetCoverUrl: "",
    Init: function () {
        //插入回调
        window.SetUrl = function (filePath) {
            $.post(Album.SaveImageItemPost
            , { rnd: Math.random, filePath: filePath, ProductId: Album.ProductId }
            , function (result) {
                eval("var Message=" + result);
                if (Message.Status != 1000)
                    alert(Message.Body);
                else
                    location.reload();
            });
        }
        // 上传回调
        window.parent.OnUploadCompleted = function (a, b) {
            if (b)
                window.SetUrl(b);
            else
                location.reload();

        }
        $("form").ajaxForm({
            beforeSerialize: function (frm) { if (!$(frm).find("input[type='file']").val()) return false; },
            sucess: function (data) { alert(data); }
        });
        // 插入新图片
        $(":input[name='btnInsertNew']").click(function () {
            window.open(Album.FileBrowserUrl);
        });
        // 设置封面
        $(":input[name='btnSetCover']").click(function () {
            var oCheckList = $("#albumListContent :checked");
            if (oCheckList.length != 1) { alert("必须并且只能选取一张图片作为封面!"); return false; }
            $.post(Album.SetCoverUrl, { rnd: Math.random(), imageId: oCheckList.val() }, function (result) { eval("var Message=" + result); if (Message.Status != 1000) alert(Message.Body); else location.reload(); });

        });
        // 点击图片修改图片
        $("#albumListContent img").click(function () {
            var imageId = $(this).parent().find(":input[name='ImageId']").val();
            var imageName = $(this).parent().find(":input[name='ImageName']").val();
            var isCover = $(this).parent().find(":checkbox").attr("checked");
            window["SetUrl"] = function (filePath) {
                $.post(Album.SaveImageItemPost, { isCover: isCover, imageId: imageId, imageName: imageName, rnd: Math.random, filePath: filePath, ProductId: Album.ProductId }, function (result) { eval("var Message=" + result); if (Message.Status != 1000) alert(Message.Body); else location.reload(); });
            }
            window.open(Album.FileBrowserUrl);
        });
        // 批量删除图片
        $(":input[name='btnDelete']").click(function () {
            //由于封面图默认是被选中的，因此需要把第一张的勾选去掉
            var allCheeck = this.checked;
            var chkList = $("#albumListContent :checked[name='isCover']");
            var vals = [];
            $.each(chkList, function (i, n) { vals[i] = n.value; });
            if (confirm("删除警告:确定要删除吗?\r\n该操将作从服务器彻底删除图片且不可恢复，请谨慎操作\r\n注意:无论你是否有勾选封面,删除图片都不会删除封面,只有在删除商品的时候才会删除封面!"))
                $.post(Album.DeleteImagePost, { rnd: Math.random(), ImageId: vals }, function (result) { eval("var Message=" + result); if (Message.Status != 1000) alert(Message.Body); else location.reload(); });

        });
        // 全选反选
        $(":checkbox[name='ckSelectAll']").click(function () {
            var allCheeck = this.checked;
            var chkList = $("#albumListContent :checkbox[name='isCover']");
            $.each(chkList, function (i, n) {
                n.checked = allCheeck;
            });
        });

        // 下一步
        $(":input[name='btnNext']").click(function () { location.href = Album.NextUrl; });
        //完成预览
        $(":input[name='btnFinish']").click(function () { location.href = Album.FinishUrl; });
        // 上一步
        $(":input[name='btnFront']").click(function () { location.href = Album.FrontUrl; });
        // 批量改图
        $(":input[name='btnModify']").click(function () {
            var post = $("<form action='' method='post' class='albumItemPost'></form>");
            var list = $("<ul ></ul>");
            var itemCount = 0;
            $("#albumListContent :checked").parent().parent().find("img").each(function (i, n) {
                itemCount++;
                var item = "<li" + ((i % 2) != 0 ? " class='odd'" : "") + ">";
                item += "<div class='img'>";
                item += n.outerHTML;
                item += "</div>";
                item += "<div class='desc'>";
                item += "<input type='hidden' name='imageId' value='" + $(n).parent().find(":hidden[name='ImageId']").val() + "' />";
                item += "<p><span>标题:</span><textarea name='tbTitle' rows='5' cols='50'>" + n.title + "</textarea></p>";
                item += "<p><span>排序:</span><input type='text' name='sequence' value='" + $(n).parent().find(":hidden[name='Sequence']").val() + "' /></p>";
                item += "<p><span>描述:</span><textarea name='description' rows='5' cols='50'>" + n.alt + "</textarea></p>";
                item += "</div>";
                item += "</li>";
                list.append(item);
            });
            post.append(list);
            if (itemCount > 0)
                post.dialog({
                    width: 900
                    , height: itemCount > 4 ? $(document).height() - 100 : "auto"
                    , closeText: "×"
                    , title: "描述/排序 -- 网页对话框"
                    , modal: true
                    , buttons: {
                        "保存": function () { $(this).ajaxSubmit({ url: Album.ItemUpdatePost, success: function () { location.reload(); } }); }
                    }
                    , close: function () { $(this).dialog("destroy") }
                });
        });
    }

}

/*-----------------------------列表-----------------------------------------------*/
var ProductList =
{
    EditUrl: "", PreUrl: "", DelUrl: "",
    Init: function () {
        // 交替行变色
        var old = $(".productListContent:odd").attr("class");
        $("#ullist li:odd").attr("class", "alter");
        //编辑按钮
        $(":button[name='btnEdit']").click(function () {
            var productId = $(this).parent().find(":input['name='ProductId'']").val();
            window.open(ProductList.EditUrl.replace("-productid-", productId));
        });
        //预览按钮
        $(":button[name='btnPreview']").click(function () {
            var productId = $(this).parent().find(":input['name='ProductId'']").val();
            window.open(ProductList.PreUrl.replace("-productid-", productId));
        });
        // 删除按钮
        $(":button[name='btnDelete']").click(function () {
            var productId = $(this).parent().find(":input['name='ProductId'']").val();
            if (confirm("确定要删除吗，该操作将彻底删除商品(包括已有的属性、图片)且不可恢复,如果你只是要将商品下线，请使用更新商品激活状态功能！"))
                $.post(ProductList.DelUrl, { productId: productId }, function () {
                    location.reload();
                });
        });
        // 单击产品名称行选中
        $(":checkbox[name='ckSelect']").each(function (i, n) {
            $(n).parent().css("cursor", "pointer").click(function () {
                $(n).attr("checked", !$(n).attr("checked"));
            });
            $(n).click(function () { $t.stopPropagation(); });
        });
        // 全选
        $("#cbSelectAll").click(function () {
            var allCheeck = this.checked;
            var chkList = $(":checkbox[name='ckSelect']");
            $.each(chkList, function (i, n) { n.checked = allCheeck; });
        });
        //所有目前被勾选中的
        var Ids = function () {
            var ids = [], chkList = $(":checked[name='ckSelect']");
            $.each(chkList, function (i, n) { ids[i] = n.value; });
            return ids;
        }
        var FormAction = function (flag) {
            var form = $("#batchForm");
            form.find(":hidden").val(flag);
            form.submit();
        }
        // 批量统一编辑
        $(":button[name='btnBatch0']").click(function () { FormAction(false); });
        // 批量逐个编辑
        $(":button[name='btnBatch1']").click(function () { FormAction(true) });
    }
}


/*------------------------------扩展属性--------------------------------------------*/
var ExtendProperty =
{
    DeleteUrl: "",
    Init: function () {
        $("#btnNext").click(function () { $("#frm1").attr("action", "?next=next"); });
        $("#btnFront").click(function () { $("#frm1").attr("action", "?front=front"); });
        var ExtendProperty = this;
        $(":input[name='btnDelete']").click(function () {
            var extendId = $(this).parent().find(":hidden[name='ExtendId']").val()
            if (confirm("删除警告\n确定要删除吗？")) {
                $.get(ExtendProperty.DeleteUrl, { extendId: extendId, rnd: Math.random() }, function () {
                    location.replace(document.URL)
                });
            }
        });
        /*
        function setValue() {
        var oElement = $(this);
        oElement.parent().find(":hidden").val("DownLoad");
        window["SetUrl"] = function(filePath) {
        oElement.val(filePath);
        }
        window.open("/WebResources/ckfinder/ckfinder.html");
        }
        $(":text[name='ExtendValue']").click(setValue);
        $(":text[name='ExtendValue']").keypress(function() { return false; });
        */
        $("#btnInsert").click(function () {
            var oTR = $("<tr class=\"textCenter\"> <td> <input name=\"ExtendKey\" type=\"text\" value=\"\" /> </td> <td> <input class=\"width50\" name=\"Sequence\" type=\"text\" value=\"0\" /> </td> <td>  <input name=\"Template\" type=\"hidden\" /><input name=\"ExtendValue\"  style='width:150px;' type=\"text\" value=\"\" /> </td> <td class=\"textLeft\"> <input name=\"ExtendId\" type=\"hidden\" value=\"0\" /> <input type=\"button\" value=\"删除\" name=\"btnDelete\" /> <input type=\"submit\" value=\"保存\" /> </td> </tr>");
            /*
            $(oTR).find(":text[name='ExtendValue']").click(setValue);
            (oTR).find(":text[name='ExtendValue']").keypress(function() { return false; });
            */
            $(oTR).find(":input[name='btnDelete']").click(function () {
                if (confirm("删除警告\n确定要删除吗？")) {
                    $(this).parent().parent().remove();
                }
            });
            $("#table1 tbody").append(oTR);
            $("#table1 tbody td :input[name='Sequence']").each(function (i, n) {
                $(n).val(i);
            });
        });
    }
}

/*-------------------------------部件-------------------------------------------*/
var Part =
{
    RelatePostUrl: "",
    HomeParDeletetUrl: "",
    HomePartPostUrl: "",
    SelectProductUrl: "",
    Reload: function () { },
    //相关产品初始化
    RelateInit: function () {
        // 修改产看按钮
        $(":button[name='btnModify']").click(function () {
            var btm = this;
            Part.Reload = function () { $(btm).click(); }
            var moduleKey = $(this).parent().find(":hidden['ModuleKey']").val();
            $.get(Part.RelatePostUrl, { rnd: Math.random(), ProductId: Relate.ProductId, moduleKey: moduleKey }, Part.PartPost);
        });
    },
    // 首页模块初始化
    HomePartInit: function () {
        Part.Reload = function () { $("#homePart").tabs("load", 3); }
        $("form").ajaxForm(
        {
            beforeSerialize: function (frm) {
                $(frm).find(":hidden[name='IsValid']").each(function (i, n) {
                    var o = $(n).parent().find(":checkbox");
                    $(n).val(o.attr("checked"));
                    o.attr("disabled", true);
                });
            }
             , success: Part.Reload
        });
        // 删除事件
        $(":button[name='delete']").click(function () {
            var partId = $(this).parent().find(":hidden[name='PartId']").val();
            if (confirm("删除警告，该操作将会导致前台页面显示不完整，请谨慎操作!"))
                $.post(Part.HomeParDeletetUrl, { partId: partId }, Part.Reload);
        });
        // 增加改变输入框
        $(":button[name='btnInsert'] ,:button[name='btnModify']").click(function () {
            var partId = $(this).parent().find(":hidden[name='PartId']").val();
            $.get(Part.HomePartPostUrl, { rnd: Math.random(), partId: partId }, Part.PartPost);
        });
    },
    PartPost: function (result) {
        result = $(result);
        var temp = [];
        // 改变类型
        $(result).find("#PartType").change(function () {
            if (this.value < 0) { $(result).find(".t1 tbody[char='tbody1']").html($(result).find(".t2 tbody[char='tbody1']").html()); }
            else { $(result).find(".t1 tbody[char='tbody1']").html($(result).find(".t2 tbody[char='tbody2']").html()); }
            $(result).find(".u1 li").remove();
            if (temp[this.value]) $(result).find(".u1").html(temp[this.value]);
        }).click(function () { temp[this.value] = $(result).find(".u1").html(); });
        //选择事件
        window.Selected = function (ids) { $(ids).each(function (i, n) { $(result).find(".u1").append($("<li><input type='button' name='btnRemove' value='移除' />&nbsp;" + n + "</li>")); }); };
        //拖拽排序
        $(result).find(".u1").sortable({ placeholder: 'ui-state-highlight' });
        //删除按钮
        $(result).find(":button[name='btnRemove']").live("click", function () { $(this).parent().remove(); return false; });
        // 选择按钮
        $(result).find(":button[name='btnSelect']").live("click", function () { window.open(Part.SelectProductUrl); return false; });
        //弹出对话框
        $(result).dialog({ title: "模块配置", width: 900, closeText: "×", close: function () { $(this).dialog("destroy"); }, bgiframe: true, modal: true }).find("form").submit(function () { $(this).ajaxSubmit({ success: function (data) { Part.Reload(); result.dialog("destroy"); } }); return false; });
    }
}
/*------------------------相关配套------------------------------------------*/
var Relate =
{
    RelatePostUrl: "",
    ProductId: "",
    SelectProductUrl: "",
    Init: function () {
        // 相同/类似产品
        $(":button[name='btnRelate']").click(function () {
            var btm = this;
            $.get(Relate.RelatePostUrl, { rnd: Math.random(), ProductId: Relate.ProductId },
              function (result) {
                  result = $(result);
                  //拖拽排序
                  result.find(".u1").sortable({ placeholder: 'ui-state-highlight' });
                  // 选择事件
                  window.Selected = function (ids) { $(ids).each(function (i, n) { $(result).find(".u1").append($("<li><input type='button' name='btnRemove' value='移除' /> " + n + "</li>")); }); };
                  //删除按钮
                  result.find(":button[name='btnRemove']").live("click", function () { $(this).parent().remove(); return false; });
                  // 选择按钮
                  result.find("#btnSelect").click(function () { window.open(Relate.SelectProductUrl); });
                  //弹出对话框
                  result.dialog({ title: "查看/修改/选择产品", width: 900, modal: true, bgiframe: true, closeText: "×", close: function () { $(this).dialog("destroy") } });
                  // ajax submit
                  result.find("form").ajaxForm(function () { result.dialog("close"); $(btm).click(); });
              });
        });
    }
}
/*-------------------------产品属性------------------------------*/
var Porperty =
{
    ChangeClassUrl: "",
    SaveUrl: "",
    PropertyValUrl: "",
    InIt: function () {
        // 上一步
        $("#btnFront").click(function () { $("#fr1").attr("action", Porperty.SaveUrl + "?front=true"); $("#fr1").submit(); });
        // 下一步
        $("#btnNext").click(function () { $("#fr1").attr("action", Porperty.SaveUrl + "?next=true"); $("#fr1").submit(); });
        // 保存
        $("#btnSave").click(function () { $("#fr1").attr("action", Porperty.SaveUrl); $("#fr1").submit(); });
        // 选择属性
        $(":input[name='PropertyValName']").live("click", function () { Porperty.PoputPropertyVal(this); return false; });
        // 选择属性
        $(":input[name='PropertyValName']").live("keypress", function () { Porperty.PoputPropertyVal(); return false; });
        // 改变分类
        $("#ClassId").change(function () {
            var productId = $(this).parent().find(":hidden[name='ProductId']").val();
            $.post(Porperty.ChangeClassUrl, { ProductId: productId, ClassId: this.value }, function (result) { $("#fr1 table tbody[char='tbody2']").html(result) });
        });
    },
    //弹出属性值窗口
    PoputPropertyVal: function (oElement) {
        window["oDialog"] = null;
        while (oElement.nodeName != "TR") { oElement = oElement.parentNode; }
        var propertyId = $(oElement).find(":input[name='PropertyId']").val();
        var cid = $(oElement).find(":hidden[name='PropertyValId']").val();
        $.get(Porperty.PropertyValUrl, { propertyId: propertyId, cid: cid, rnd: Math.random() }, function (result) {
            window["SetPropertyVal"] = function (valId, value) {
                $(oElement).find(":input[name='PropertyValName']").val(value);
                $(oElement).find(":input[name='PropertyValId']").val(valId);
                window["oDialog"].dialog("destroy");
            }
            //弹出对话框
            if (!document.getElementById("pop-content"))
                $(document.body).append("<div id='pop-content'></div>");
            $("#pop-content").html(result);
            //dialog
            window["oDialog"] = $("#pop-content").dialog({ width: 200, title: "属性选择器", bgiframe: true, modal: true, closeText: "×", close: function () { $(this).dialog("destroy"); } });
        });

    }
}

